package com.hnkywl.sts.action.kcgl;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.kcgl.LldmxDao;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.entity.xsgl.Lld;
import com.hnkywl.sts.service.kcgl.LldService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-15
 * Time: 下午2:17
 * To change this template use File | Settings | File Templates.
 */

@Namespace("/kcgl/nbll")
public class LldAction extends SimpleActionSupport {
    private LldService lldService;
    //页面当前位置
    private int page;
    //每页行数
    private int rows;
    //行索引
    private int index;
    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;
    //起始发货时间
    private Date kdrqStart;
    //结束发货时间
    private Date kdrqEnd;
    //发料公司ID
    private Long flgsId;
    //发料公司简称
    private String flgsJc;

    //当前登录员工的公司ID
    private Long dlgsId;
    private Long dlzggsId;
    private Yg dlyg;
    //领料单
    private Lld lld;
    //领料单ID
    private Long lldId;
    //状态
    private String zt;
    //领料单明细
    private LldmxDao lldmxDao;

    @Action("nbll")
    public String nbll() {
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    //领料单列表
    @Action("nbllList")
    public void lldList() {
        this.renderJson(this.lldService.findAllLld(zt, kdrqStart, kdrqEnd, flgsId, flgsJc, page, rows));
    }

    @Actions({@Action("nbll_content"), @Action("nbll_blcontent"), @Action("nbll_detail")})
    public String nbllContent() {
        if (lldId != null) {
            lld = this.lldService.findLldDetail(lldId);
        } else {
            dlyg = YgService.getCurrentYg();
            dlgsId = YgService.getCurrentYgGsId();
            dlzggsId = YgService.getCurrentYgZgGsId();
            lld = new Lld();
        }
        return SUCCESS;
    }

    //领料申请
    @Action("nbll_llsq")
    public void llsqLld() {
        try {
            lldService.saveLld(lld);
            renderText("{\"statusCode\":\"200\", \"message\":\"保存领料单信息成功\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //领料办理
    @Action("nbll_bl")
    public void blLld() {
        try {
            this.lldService.blLld(lld);
            renderText("{\"statusCode\":\"200\", \"message\":\"发料成功！\", \"operationType\":\"update\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //领料单明细列表
    @Action("nbll_lldmx")
    public void lldmxDetail() {
        this.renderJson(this.lldService.findLldmxDetail(lldId));
    }

    //领料单审核
    @Action("nbll_sh")
    public void changeStatus() {
        try {
            lldService.changeStatus(lldId);
            renderText("{\"statusCode\":\"200\", \"message\":\"领料单已审核！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }

    }

    //领料单明细打印查询
    @Action("nbll_print_detail")
    public void nbllPrintDetail() {
        Gson gson = new Gson();
        List<Map> lldmxList = this.lldmxDao.findLldmxList(lldId);
        Double yfsl = 0d;
        Double sfsl = 0d;
        Double lyje = 0d;
        String result = "";
        java.text.DecimalFormat df = new java.text.DecimalFormat("0.0000");
        java.text.DecimalFormat jtdf = new java.text.DecimalFormat("0.00");
        StringBuffer sbTable = new StringBuffer();
        if (null != lldmxList) {
            sbTable.append("<table id = \"lldmxdetail\" class=\"tb intb\"><thead>");
            sbTable.append("<tr>");
            sbTable.append("<td>物资编码</td><td>物资名称</td><td>型号规格</td><td>单位</td><td>材质</td>" +
                    " <td>图号</td> <td>应发数量</td><td>实发数量</td><td>领用单价</td><td>领用总金额</td><td>备注</td>");
            sbTable.append("</tr></thead><tbody>");

            for (int i = 0; i < lldmxList.size(); i++) {
                sbTable.append("<tr>");
                // 物资编码
                sbTable.append("<td>");
                if (null != lldmxList.get(i).get("jtwz.oldcode")) {
                    sbTable.append(lldmxList.get(i).get("jtwz.oldcode"));
                }
                sbTable.append("</td>");
                //    品名
                sbTable.append("<td>");
                if (null != lldmxList.get(i).get("jtwz.name")) {
                    sbTable.append(lldmxList.get(i).get("jtwz.name"));
                }
                sbTable.append("</td>");
                //  规格型号
                sbTable.append("<td>");
                if (null != lldmxList.get(i).get("jtwz.ggxh")) {
                    sbTable.append(lldmxList.get(i).get("jtwz.ggxh"));
                }
                sbTable.append("</td>");
                //单位
                sbTable.append("<td>");
                if (null != lldmxList.get(i).get("jtwz.dw")) {
                    sbTable.append(lldmxList.get(i).get("jtwz.dw"));
                }
                sbTable.append("</td>");
                //材质
                sbTable.append("<td>");
                if (null != lldmxList.get(i).get("jtwz.cz")) {
                    sbTable.append(lldmxList.get(i).get("jtwz.cz"));
                }
                sbTable.append("</td>");
                //图号
                sbTable.append("<td>");
                if (null != lldmxList.get(i).get("jtwz.th")) {
                    sbTable.append(lldmxList.get(i).get("jtwz.th"));
                }
                sbTable.append("</td>");
                // 应发数量
                sbTable.append("<td>");
                if (null != lldmxList.get(i).get("kdsl")) {
                    Double mkdsl = (Double) lldmxList.get(i).get("kdsl");
                    BigDecimal bkdsl = new BigDecimal(mkdsl);
                    Double dkdsl = bkdsl.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                    sbTable.append(df.format(dkdsl));
                    yfsl += dkdsl;
                }
                sbTable.append("</td>");
                // 实发数量
                sbTable.append("<td>");
                if (null != lldmxList.get(i).get("flsl")) {
                    Double mflsl = (Double) lldmxList.get(i).get("flsl");
                    BigDecimal bflsl = new BigDecimal(mflsl);
                    Double dflsl = bflsl.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                    sbTable.append(df.format(dflsl));
                    sfsl += dflsl;
                }
                sbTable.append("</td>");
                //领用单价
                sbTable.append("<td>");
                if (null != lldmxList.get(i).get("kddj")) {
                    Double mcbdj = (Double) lldmxList.get(i).get("kddj");
                    BigDecimal bcbdj = new BigDecimal(mcbdj);
                    Double dcbdj = bcbdj.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                    sbTable.append(df.format(dcbdj));
                }
                sbTable.append("</td>");
                // 领用金额
                sbTable.append("<td>");
                if (null != lldmxList.get(i).get("flje")) {
                    Double mcbje = (Double) lldmxList.get(i).get("flje");
                    BigDecimal bcbje = new BigDecimal(mcbje);
                    Double dcbje = bcbje.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                    sbTable.append(jtdf.format(dcbje));
                    lyje += dcbje;
                }
                sbTable.append("</td>");
                //备注
                sbTable.append("<td>");
                if (null != lldmxList.get(i).get("llbz")) {
                    sbTable.append(lldmxList.get(i).get("llbz"));
                }
                sbTable.append("</td>");

                sbTable.append("</tr>");
            }
            sbTable.append("<tr>");

            sbTable.append("<th colspan=\"6\">合计:");
            sbTable.append("</th>");

            sbTable.append("<td>");
            sbTable.append(df.format(yfsl));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(sfsl));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(lyje));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("</tr>");
            sbTable.append("</tbody></table>");
        }
        renderHtml(sbTable.toString());
    }
    //=================get and set=======================

    public String getZt() {
        return zt;
    }

    public void setZt(String zt) {
        this.zt = zt;
    }

    public Long getDlgsId() {
        return dlgsId;
    }

    public void setDlgsId(Long dlgsId) {
        this.dlgsId = dlgsId;
    }

    public Long getDlzggsId() {
        return dlzggsId;
    }

    public void setDlzggsId(Long dlzggsId) {
        this.dlzggsId = dlzggsId;
    }

    public Yg getDlyg() {
        return dlyg;
    }

    public void setDlyg(Yg dlyg) {
        this.dlyg = dlyg;
    }

    public Lld getLld() {
        return lld;
    }

    public void setLld(Lld lld) {
        this.lld = lld;
    }

    public Long getLldId() {
        return lldId;
    }

    public void setLldId(Long lldId) {
        this.lldId = lldId;
    }

    public Date getKdrqStart() {
        return kdrqStart;
    }

    public void setKdrqStart(Date kdrqStart) {
        this.kdrqStart = kdrqStart;
    }

    public Date getKdrqEnd() {
        return kdrqEnd;
    }

    public void setKdrqEnd(Date kdrqEnd) {
        this.kdrqEnd = kdrqEnd;
    }

    public Long getFlgsId() {
        return flgsId;
    }

    public void setFlgsId(Long flgsId) {
        this.flgsId = flgsId;
    }

    public String getFlgsJc() {
        return flgsJc;
    }

    public void setFlgsJc(String flgsJc) {
        this.flgsJc = flgsJc;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public GnczService getGnczService() {
        return gnczService;
    }

    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setLldService(LldService lldService) {
        this.lldService = lldService;
    }

    @Autowired
    public void setLldmxDao(LldmxDao lldmxDao) {
        this.lldmxDao = lldmxDao;
    }
}
